<?php
namespace Admin\Model;
use Think\Model;
use Common\Common\ConstantUtil;
use Common\Common\UploadUtil;
class ColumnModel extends  Model{

    protected $tableName = 'wzjs_develop_column';


    /**
     * 栏目添加
     * @param $post
     * @return bool
     */
    public  function add($post) {
        $uploadImg = A("Upload")->image();
        $model = M();
        try{
            $model->startTrans();
            $date = date("Y-m-d H:i:s");
            $column['name']                 = $post["name"];
            $column['sort']                 = empty($post["sort"]) ? 1  : $post["sort"];
            $column['content']              = empty($post["editorValue"]) ? ""  : $post["editorValue"];
            $column['code']                 = empty($post["code"]) ? ""  : $post["code"];
            $column['logo']                 = empty($post["logo"]) ? ""  : $post["logo"];
            $column['show']                 = $post["show"];
            if(!empty($uploadImg["title_img"]) ) {
                $column['title_img']            = "/".UploadUtil::UPLOAD_PATH."/".$uploadImg["title_img"]["savepath"].$uploadImg["title_img"]["savename"];
            }
            $column['create_time']          = $date;
            $column_id = $model->table('wzjs_develop_column')->add($column);
            $data["column_id"]      = $column_id;
            $data["column_parent"]  = $post["parent_id"];
            $data["create_time"]    = $date;
            $rank                   = M("wzjs_develop_column_relation")->where(["column_id"=>$data["column_parent"]])->find();
            $data["rank"]           = $rank["rank"]+1;
            $data["number"]         = $rank["number"].sprintf("%04d", $data["column_id"]);
            $model->table('wzjs_develop_column_relation')->add($data);
            $model->commit();
            return true;
        } catch (\PDOException $e) {
            $model->rollback();
            return false;
        }

    }

    public static  function getName($id) {
        if(empty($id)) return false;
        return self::where("id","=",$id)->pluck("name");
    }


}